package imageswipe.navigation 
{
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.geom.Point;
	import imageswipe.events.SelectIndexEvent;
	import imageswipe.events.SwipeEvent;
	import imageswipe.ImageSwipe;
	import imageswipe.navigation.dot.DotIndicator;
	import imageswipe.vo.NavigationVO;
	
	/**
	 * ...
	 * @author 
	 */
	[Event(name = "indexChanged", type = "imageswipe.events.SelectIndexEvent")]
	public class SwipeNavigation extends Sprite 
	{
		private var _dotIndicator			: DotIndicator;
		private var _dataProvider			: NavigationVO;
		private var _animationInProgress	: Boolean;
		private var _imageSwipe				: ImageSwipe;
		private var _mask					: Shape;
		
		public function SwipeNavigation() 
		{
			_dotIndicator = new DotIndicator;
			_imageSwipe = new ImageSwipe;
			
			_dotIndicator.addEventListener(SelectIndexEvent.INDEX_CHANGED, _onIndexChanged);
			_imageSwipe.addEventListener(SelectIndexEvent.INDEX_CHANGED, _onIndexChanged);
			_imageSwipe.addEventListener(SwipeEvent.ANIMATION_IN_PROGRESS, _onAnimationInProgress);
			
			addChild(_imageSwipe);
			addChild(_dotIndicator);
		}
		
		private function _onAnimationInProgress(e:SwipeEvent):void 
		{
			_animationInProgress = e.animationInProgress;
		}
		
		private function _onIndexChanged(e:SelectIndexEvent):void 
		{
			//trace(this, "index changed to::", e.index);
			//setNavigationIndex(e.index);
			dispatchEvent(e);
			
			_imageSwipe.setImageIndex(e.index);
			setNavigationIndex(e.index);
		}
		
		public function setNavigationIndex(index:uint):void 
		{
			if (_animationInProgress) return;
			_dataProvider.dotIndicatorVO.indexSelected = index;
			_draw();
		}
		
		private function _draw():void 
		{
			if (!_dataProvider || !_dataProvider is NavigationVO) return;
			
			_dotIndicator.dataProvider = _dataProvider.dotIndicatorVO;
			_imageSwipe.dataProvider = _dataProvider.imageSwipeVO;
			
			_mask = new Shape;
			_mask.graphics.beginFill(0x000000, .4);
			_mask.graphics.drawRect(0, 0, _dataProvider.imageSwipeVO.imageWidth, _dataProvider.imageSwipeVO.imageHeight);
			_mask.graphics.endFill();
			this.mask = _mask;
			
		}
		
		public function set dataProvider(value:NavigationVO):void 
		{
			if (!value || !value is NavigationVO) return;
			_dataProvider = value;
			_draw();
		}
		
		public function dotIndicatorWidth():Number
		{
			if (_dotIndicator) 
			{
				return _dotIndicator.width;
			}
			return 0;
		}
		
		public function setNavigationPosition(point:Point):void
		{
			if (_dotIndicator) 
			{
				_dotIndicator.x = point.x;
				_dotIndicator.y = point.y;
			}
		}
	}

}